﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using EmailHelper.tool;
using EmailHelper.model;

namespace EmailHelper.view
{

    public delegate void setValue(List<EmailsModel> emailsModels);

    /// <summary>
    /// GetEmailsFromDataBase.xaml 的交互逻辑
    /// </summary>
    public partial class GetEmailsFromDataBase : Window
    {

        public event setValue setValueHandler;

        public GetEmailsFromDataBase()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public void judgeMethod()
        {
            if ((bool)RB_Windows.IsChecked)
            {
                SqlHelper.IsWindowsMethos = true;
            }
            else
            {
                SqlHelper.IsWindowsMethos = false;
            }
        }

        private void btn_Test_Click(object sender, RoutedEventArgs e)
        {
            judgeMethod();
            try
            {
                SqlHelper.resource = $"Data Source={TB_ServerName.Text.ToString()};Initial Catalog={TB_DataBaseName.Text.ToString()};Integrated Security=True";
                SqlHelper.IPResource = $"Data Source={TB_ServerName.Text.ToString()};Initial Catalog={TB_DataBaseName.Text.ToString()};User ID={TB_UserName.Text.ToString()};Password={PB_UserPassword.Password.ToString()}";
                if (SqlHelper.Connection.State == System.Data.ConnectionState.Open)
                {
                    MessageBox.Show("连接成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show($"连接失败\n原因：{SqlHelper.Connection.State}", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show($"连接失败\n原因：{ex.Message}", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            
        }

       
        /// <summary>
        /// 获取数据库中的数据
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ok_Click(object sender, RoutedEventArgs e)
        {
            int count = 1;
            List<EmailsModel> emailsModels = new List<EmailsModel>(); 
            judgeMethod();
            SqlHelper.resource = $"Data Source={TB_ServerName.Text.ToString()};Initial Catalog={TB_DataBaseName.Text.ToString()};Integrated Security=True";
            SqlHelper.IPResource = $"Data Source={TB_ServerName.Text.ToString()};Initial Catalog={TB_DataBaseName.Text.ToString()};User ID={TB_UserName.Text.ToString()};Password={PB_UserPassword.Password.ToString()}";
            DataTable table = SqlHelper.Select(TB_TableName.Text.ToString(), TB_columnName.Text.ToString());
            

            for (int i = 0; i < table.Rows.Count; i++)
            {
                String temp = table.Rows[i][TB_columnName.Text.ToString()].ToString();
                if (temp.Contains("@")&&temp.Contains('.'))
                {
                    emailsModels.Add(new EmailsModel() { Email = temp, Count = count });
                    count++;
                }
            }

            setValueHandler(emailsModels);
            this.Close();
            return;

        }

        public void sendValueToParent(List<EmailsModel> emailsModels)
        {

        }
    }
}
